package co.fitstart.admin.web.controller.user.satisfaction;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.SatisfactionSurveyService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.market.satisfaction.SatisfactionSurvey;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 满意度调查问卷填写情况
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("satisfaction-surveys")
@MenuSetting("menu-user")
public class SatisfactionSurveyController {

    @Autowired
    private SiteService siteService;

    @Autowired
    private PrincipalService principalService;

    @Autowired
    private SatisfactionSurveyService satisfactionSurveyService;

    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-satisfaction-survey-management")
    public ModelAndView list(@Secure Login login,
                             @RequestParam(value = "siteId", required = false) Long siteId,
                             @RequestParam(value = "complete", required = false) Boolean complete,
                             Page page,
                             HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("market/satisfaction-survey/satisfaction-survey-list");
        
        Site site = null;
        if(siteId != null) {
            site = siteService.load(siteId);
        }
        
        List<SatisfactionSurvey> surveys = new ArrayList<>();
        
        if(login.isRoot() || login.isAdmin()) {
            surveys = satisfactionSurveyService.list(site, complete, page);
        } else if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            List<Site> sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                if(site == null) {
                    surveys = satisfactionSurveyService.list(sites, complete, page);
                } else if(sites.contains(site)) {
                    surveys = satisfactionSurveyService.list(site, complete, page);
                }
            }
        } else {
            surveys = satisfactionSurveyService.list(login.getSite(), complete, page);
        }
        
        modelAndView.addObject("siteId", siteId);
        modelAndView.addObject("complete", complete);
        modelAndView.addObject("surveys", surveys);
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }

    /**
     * 
     * @param login
     * @param surveyId
     * @return
     */
    @RequestMapping(value = "/{surveyId}", method = RequestMethod.GET)
    @MenuSetting("submenu-satisfaction-survey-management")
    public ModelAndView view(@Secure Login login,
                             @PathVariable("surveyId") Long surveyId) {
        ModelAndView modelAndView = new ModelAndView("market/satisfaction-survey/satisfaction-survey-view");
        
        if(login.isRoot() || login.isAdmin()) {
            SatisfactionSurvey survey = satisfactionSurveyService.load(surveyId);
            
            modelAndView.addObject("survey", survey);
        }
        
        return modelAndView;
    }

    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    @MenuSetting("submenu-satisfaction-survey-management")
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("market/satisfaction-survey/satisfaction-survey-export");
        
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param siteId
     * @param beginDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login,
                                 @RequestParam(value = "siteId", required=false) Long siteId,
                                 @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                 @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("satisfaction-survey-report.xls"));
        
        List<SatisfactionSurvey> surveys = new ArrayList<>();
        
        if(login.isRoot() || login.isAdmin()) {
            Site site = null;
            if(siteId != null) {
                site = siteService.load(siteId);
            }
            
            if(beginDate == null || endDate == null) {
                beginDate = new Date();
                endDate = new Date();
            }
            
            surveys = satisfactionSurveyService.listComplete(site, beginDate, endDate);
            
            modelAndView.addObject("surveys", surveys);
            modelAndView.addObject("beginDate", DateFormatUtils.format(beginDate, "yyyy-MM-dd"));
            modelAndView.addObject("endDate", DateFormatUtils.format(endDate, "yyyy-MM-dd"));
            
            DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            modelAndView.addObject("fmt", fmt);
        }
        
        return modelAndView;
    }
}
